layui.config({
    base : "../../js/"
}).extend({
    "jqCommon" : "jqCommon"
})
layui.use(['form','layer','table','laytpl','jqCommon','util'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;
        jqcommon = layui.jqCommon;
        util = layui.util;

    //用户列表
    var tableIns = table.render({
        elem: '#vipOrderList',
        url : 'http://mini.yantaibaixing.cn/orders',
        method: 'GET', //如果无需自定义HTTP类型，可不加该参数
        response: {
            statusName: 'code' //数据状态的字段名称，默认：code
            ,statusCode: 200 //成功的状态码，默认：0
            ,msgName: 'message' //状态信息的字段名称，默认：msg
            ,countName: 'total' //数据总数的字段名称，默认：count
            ,dataName: 'datas' //数据列表的字段名称，默认：data
        },
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 20,
        id : "vipOrderListTable",
        cols : [[
            {field: 'name', title: '姓名', align:"center"},
            {field: 'phone', title: '手机号', align:"center"},
            {field: 'vipName', title: '会员名称', align:"center"},
            {field: 'amount', title: '支付金额', align:'center',templet:function(d){
                return (d.amount/100).toFixed(0) + "元";
                }},
            {field: 'channelId', title: '支付方式', align:'center',templet:function(d){
                    if(d.channelId == "WX_MINI"){
                        return "小程序付款"
                    }
                    if(d.channelId == "WX_JSAPI"){
                        return "公众号付款"
                    }
                    if(d.channelId == "WX_NATIVE"){
                        return "微信扫码付款"
                    }
                }},
            {field: 'createTime', title: '订单创建时间', align:'center',templet:function(d){
                return util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss');
            }},
            {field: 'paySuccessTime', title: '支付成功时间', align:'center',templet:function(d){
                if(d.paySuccessTime == "无"){
                    return "无";
                }else {
                    return util.toDateString(d.paySuccessTime, 'yyyy-MM-dd HH:mm:ss');
                }
            }},
            {field: 'status', title: '支付状态', align:'center',templet:function(d){
                if(d.status == 1){
                    return "待支付"
                }
                if(d.status == 2){
                    return "支付成功"
                }
                if(d.status == 3){
                    return "超时关闭"
                }
            }}
        ]]
    });

    //搜索【此功能需要后台配合，所以暂时没有动态效果演示】
    $(".search_btn").on("click",function(){
        var status = $("#status").val();
        var param = $(".searchVal").val();
        table.reload("vipOrderListTable",{
            page: {
                curr: 1 //重新从第 1 页开始
            },
            where: {
                param: param,  //搜索的关键字
                status: status
            }
        })
    });

    //添加用户
    function addUser(edit){
        var index = layui.layer.open({
            title : "编辑用户",
            type : 2,
            content : "userAdd.html",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(edit){
                    body.find(".id").val(edit.id);  //冗余ID
                    body.find(".type").val(edit.type);  //冗余TYPE
                    body.find(".name").val(edit.name);  //登录名
                    body.find(".phone").val(edit.phone);  //手机号
                    if(edit.shopName != '无'){
                        body.find(".shopName").val(edit.shopName);    //商户名称
                    }
                    if(edit.shopAdress != '无'){
                        body.find(".shopAdress").text(edit.shopAdress);    //商户地址
                    }
                    form.render();
                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回用户列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        layui.layer.full(index);
        window.sessionStorage.setItem("index",index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(window.sessionStorage.getItem("index"));
        })
    }
    $(".addNews_btn").click(function(){
        addUser();
    })

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('vipOrderListTable'),
            data = checkStatus.data,
            newsId = [];
        if(data.length > 0) {
            for (var i in data) {
                newsId.push(data[i].newsId);
            }
            layer.confirm('确定删除选中的用户？', {icon: 3, title: '提示信息'}, function (index) {
                // $.get("删除文章接口",{
                //     newsId : newsId  //将需要删除的newsId作为参数传入
                // },function(data){
                tableIns.reload();
                layer.close(index);
                // })
            })
        }else{
            layer.msg("请选择需要删除的用户");
        }
    })

    //列表操作
    table.on('tool(vipOrderList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
            addUser(data);
        }else if(layEvent === 'usable'){ //启用禁用
            var _this = $(this),
                usableText = "是否确定禁用此用户？",
                btnText = "已禁用";
            if(_this.text()=="已禁用"){
                usableText = "是否确定启用此用户？",
                btnText = "已启用";
            }
            layer.confirm(usableText,{
                icon: 3,
                title:'系统提示',
                cancel : function(index){
                    layer.close(index);
                }
            },function(index){
                _this.text(btnText);
                layer.close(index);
            },function(index){
                layer.close(index);
            });
        }else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此用户？',{icon:3, title:'提示信息'},function(index){
                // $.get("删除文章接口",{
                //     newsId : data.newsId  //将需要删除的newsId作为参数传入
                // },function(data){
                    tableIns.reload();
                    layer.close(index);
                // })
            });
        }
    });
})
